package com.ac.scum.log.web.model.menu.service;

import com.ac.common.core.exception.BaseException;
import com.ac.scum.log.bean.Menu;
import com.ac.scum.log.bean.UserPreference;
import com.ac.scum.log.web.model.menu.mapper.MenuMapper;
import com.ac.scum.log.web.model.user.mapper.UserPreferenceMapper;
import com.ac.scum.log.web.utils.user.CurrentUserUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class MenuService extends ServiceImpl<MenuMapper, Menu> {

    private final MenuMapper menuMapper;
    private final UserPreferenceMapper userPreferenceMapper;

    public List<Menu> findCurrentLoginUserChangeServerMenu() throws BaseException {
        UserPreference userPreference = userPreferenceMapper.selectOne(Wrappers.lambdaQuery(UserPreference.class).eq(UserPreference::getUserId, CurrentUserUtil.getCurrentLogUser()));
        return findMenuByServerId(userPreference.getLastCheckServerId());
    }

    public List<Menu> findMenuByServerId(long serverId) {
        List<Menu> menus = menuMapper.getMenuByServerId(serverId);
        if (menus.isEmpty()) {
            Menu menu = new Menu();
            menu.setId(1771060423825260546L);
            menu.setPid(0L);
            menu.setTitle("首页");
            menu.setMenuType(0);
            menu.setSortId(10);
            menu.setType(2);
            menu.setWeigh(0);
            menu.setStatus(0);
            menu.setHidden(false);
            menu.setName("首页");
            menu.setPath("/home");
            menu.setComponent("/Home");
            menu.setIsFrame(false);
            menu.setUseContainer(false);
            menus.add(menu);
        }
        return menus;
    }
}
